package com.bop.dehong.setting;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.PreferencesUtils;
import com.base.dehong.util.RegexChk;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.DeHongApp;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.http.model.UpdatePwdModel;
import com.bop.dehong.info.EmptyInfo;
import com.bop.dehong.main.MainActivity;

/**
 * 更改登录密码
* @author TangWenhan/YeFengyun
*/
public class SetPwdActivity extends BaseActivity{
	private EditText mOldNameEt,mNewNameEt,mConfirmNameEt;
	private String uid;
	private UpdatePwdModel mUpdateModel;

	@Override
	protected void initTop() {
		View mView = getView(R.id.top);
		TopManager manager = new TopManager(mView);
		manager.setTitle("登录密码");
		manager.setLeftImageVisibility(View.VISIBLE);
	}

	@Override
	protected void initUI() {
		uid = getIntent().getExtras().getString(Config.UID);
		mOldNameEt = getView(R.id.old_pwd);
		mNewNameEt = getView(R.id.new_pwd);
		mConfirmNameEt = getView(R.id.new_pwd2);
		registerOnClickListener(getView(R.id.submitpwd_btn));		
	}
	
	public void submit(){
		String oldNameStr = getStr4TextView(mOldNameEt);
		String newNameStr = getStr4TextView(mNewNameEt);
		String confirNameStr = getStr4TextView(mConfirmNameEt);
		if (TextUtils.isEmpty(oldNameStr)) {
			ToastUtils.show(this, "请填写当前密码!");
			return;
		}
		if (TextUtils.isEmpty(newNameStr)) {
			ToastUtils.show(this, "请填写新密码!");
			return;
		}
		if (!RegexChk.isPwd(newNameStr)) {
			ToastUtils.show(this, "密码由大小写字母及数字组合，6-18位字符");
			return;
		}
		if (TextUtils.isEmpty(confirNameStr)) {
			ToastUtils.show(this, "确认新密码不能为空!");
			return;
		}
		if (!newNameStr.equals(confirNameStr)) {
			ToastUtils.show(this, "新密码与确定新密码不一致！");
			return;
		}
		showProgressDialog();
		if (mUpdateModel == null) {
			mUpdateModel = new UpdatePwdModel(this, callback);
		}
		mUpdateModel.doNet(uid, oldNameStr, newNameStr);
	}
	
	RequestCallBack<EmptyInfo> callback = new RequestCallBack<EmptyInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(EmptyInfo info) {
			super.onSuccess(info);
			ToastUtils.show(SetPwdActivity.this, "密码修改成功");
			DeHongApp.mLoginEntity = null;
			DeHongApp.mUserInfo = null;
			PreferencesUtils.putString(SetPwdActivity.this, Config.UID, "");
			PreferencesUtils.putString(SetPwdActivity.this, Config.Platform, "");
			PreferencesUtils.putString(SetPwdActivity.this, Config.NickName, "");
//			PreferencesUtils.putString(this, Config.USERNAME, "");
			PreferencesUtils.putString(SetPwdActivity.this, Config.PASSWORD, "");
			String name = PreferencesUtils.getString(SetPwdActivity.this, Config.USERNAME);
			PreferencesUtils.putString(SetPwdActivity.this, Config.GESTURE_PWD + name, "");
			Intent intent = new Intent(SetPwdActivity.this, MainActivity.class);//投标
			intent.putExtra("page", 1);
			startActivity(intent);
			finishCurrentActivity();
		}
		
	};

	@Override
	protected void initData() {
		
	}

	@Override
	protected int getContentViewId() {
		TAG = getClass().getName();
		return R.layout.act_set_pwd;
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
		case R.id.submitpwd_btn:
			submit();			
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}

}

